#include "cpagectrl.h"

CPageCtrl::CPageCtrl(QWidget *parent)
    :m_parent(parent)
{
    m_btnPreviousPage = nullptr;//上一页
    m_btnNextPage = nullptr;//下一页
    m_editCurPageNum = nullptr;//当前页

    m_btnFirstPage = nullptr;//第一页
    m_btnLastPage = nullptr;//最后一页

    m_nCurPageNum = 0;
    m_nTotalPageNum = 0;
    m_nTotalItemNum = 0;

    m_pLabTotalPageCount = nullptr;

    Init();
}
CPageCtrl::~CPageCtrl()
{
    if(m_pLabTotalPageCount)
    {
        delete m_pLabTotalPageCount;
        m_pLabTotalPageCount = nullptr;
    }

    if(m_btnFirstPage)
    {
        delete m_btnFirstPage;
        m_btnFirstPage = nullptr;
    }

    if(m_btnLastPage)
    {
        delete m_btnLastPage;
        m_btnLastPage = nullptr;
    }

    if(m_btnPreviousPage)
    {
        delete m_btnPreviousPage;
        m_btnPreviousPage = nullptr;
    }

    if(m_btnNextPage)
    {
        delete m_btnNextPage;
        m_btnNextPage = nullptr;
    }
    if(m_editCurPageNum)
    {
        delete m_editCurPageNum;
        m_editCurPageNum = nullptr;
    }


}



void CPageCtrl::Init()
{
    //创建操作按钮  水平布局
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(5); //间距
    mainLayout->setContentsMargins(0,0,0,0);

    m_pLabTotalPageCount = new QLabel();
    m_pLabTotalPageCount->setFixedSize(80, 24);
    SetTotalCount(0, 0, 0);

    m_btnPreviousPage = new QPushButton();
    m_btnNextPage = new QPushButton();
    m_btnFirstPage = new QPushButton();
    m_btnLastPage = new QPushButton();
    m_editCurPageNum = new CLineEdit2();



    m_btnPreviousPage->setIcon(QIcon(":/images/PreviousPage.png"));
    m_btnNextPage->setIcon(QIcon(":/images/NextPage.png"));
    m_btnFirstPage->setIcon(QIcon(":/images/FirstPage.png"));
    m_btnLastPage->setIcon(QIcon(":/images/LastPage.png"));


    //m_btnPreviousPage->setIconSize(QSize(32, 32));
    //m_btnPreviousPage->setText("上一页");
    m_btnPreviousPage->setFixedSize(24, 24);              // 强制固定尺寸
    //m_btnNextPage->setText("下一页");
    m_btnNextPage->setFixedSize(24, 24);

    //m_btnFirstPage->setText("第一页");
    m_btnFirstPage->setFixedSize(24, 24);
   // m_btnLastPage->setText("最后一页");
    m_btnLastPage->setFixedSize(24, 24);


    //QRegularExpression regExp("^[1-9]\\d*$"); // 正则：首位非0，后续任意数字
    //m_editCurPageNum->setValidator(new QRegularExpressionValidator(regExp, this));

    m_editCurPageNum->setValidator(new QIntValidator(0, 2147483647, this));  // 限制输入0及以上整数
    m_editCurPageNum->setText("0");
    m_editCurPageNum->setFixedSize(50, 24);

    m_btnPreviousPage->setStyleSheet(
         R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    m_btnNextPage->setStyleSheet(
        R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    m_btnFirstPage->setStyleSheet(
        R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );
    m_btnLastPage->setStyleSheet(
        R"(
            QPushButton {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );

    m_editCurPageNum->setStyleSheet(
         R"(
            QLineEdit {
               border:1px solid #00BFFF;
               padding:5px;
               border-radius:5px;
            }
        )"
        );


    mainLayout->addWidget(m_pLabTotalPageCount);
    mainLayout->addWidget(m_btnFirstPage);
    mainLayout->addWidget(m_btnPreviousPage);
    mainLayout->addWidget(m_editCurPageNum);
    mainLayout->addWidget(m_btnNextPage);
    mainLayout->addWidget(m_btnLastPage);

    mainLayout->addStretch();  // 添加水平弹簧，推动两侧控件分离

    connect(m_btnPreviousPage, &QPushButton::clicked, this,&CPageCtrl::OnBtnPreviousPageClicked);
    connect(m_btnNextPage, &QPushButton::clicked, this,&CPageCtrl::OnBtnNextPageClicked);

    connect(m_btnFirstPage, &QPushButton::clicked, this,&CPageCtrl::OnBtnFirstPageClicked);
    connect(m_btnLastPage, &QPushButton::clicked, this,&CPageCtrl::OnBtnLastPageClicked);

    //仅在文本不同时触发
    connect(m_editCurPageNum, &CLineEdit2::SetTextChange, this,&CPageCtrl::OnLineEditTextChange);
    connect(m_editCurPageNum, &CLineEdit2::returnPressed, this,&CPageCtrl::OnLineEditTextEnter);

    //returnPressed() 回车消息
    //textChanged会导致输入时也 设置页数， 怎么避免，重写lineedit的增加settext2发送消息

}

void CPageCtrl::OnBtnFirstPageClicked()
{
    if(m_editCurPageNum)
    {
        m_editCurPageNum->SetText2("1");
    }
}


void CPageCtrl::OnBtnLastPageClicked()
{
    if(m_editCurPageNum)
    {
        m_editCurPageNum->SetText2(QString::number(m_nTotalPageNum, 10));
    }
}

void CPageCtrl::OnBtnPreviousPageClicked()
{
    if(m_editCurPageNum)
    {
        m_editCurPageNum->SetText2(QString::number(m_nCurPageNum-1, 10)); //SetText2会发送消息SetTextChange
    }
}

void CPageCtrl::OnBtnNextPageClicked()
{
    if(m_editCurPageNum)
    {
        m_editCurPageNum->SetText2(QString::number(m_nCurPageNum+1, 10));
    }
}

void CPageCtrl::OnLineEditTextChange(const QString &text)
{
    if(!m_editCurPageNum)
    {
        return;
    }

    int ntempnum = text.toInt();
    //发送消息
    if(ntempnum < 1)
    {
        //没有设置过数据，设置回原数据
        m_editCurPageNum->blockSignals(true);    // 阻塞所有信号发射textEdited/textChanged
        m_editCurPageNum->setText(QString::number(m_nCurPageNum, 10));
        m_editCurPageNum->blockSignals(false);   // 恢复信号发射
        return;
    }

    //超过总页数
    if(ntempnum >m_nTotalPageNum && m_nTotalPageNum != 0)
    {
        //超过总数，设置回源数据
        m_editCurPageNum->blockSignals(true);    // 阻塞所有信号发射textEdited/textChanged
        m_editCurPageNum->setText(QString::number(m_nCurPageNum, 10));
        m_editCurPageNum->blockSignals(false);   // 恢复信号发射
        return;
    }

    //页数在支持范围内，设置上一页 下一页无效  灰色
     m_nCurPageNum = ntempnum;

    if(!m_btnPreviousPage || !m_btnNextPage || !m_btnLastPage || !m_btnFirstPage)
    {
        return;
    }
    if(m_nCurPageNum == m_nTotalPageNum && m_nCurPageNum == 1)
    {
        m_btnPreviousPage->setEnabled(false);
        m_btnNextPage->setEnabled(false);
        m_btnLastPage->setEnabled(false);
        m_btnFirstPage->setEnabled(false);
    }
    else if(m_nCurPageNum == m_nTotalPageNum && m_nCurPageNum != 1)//非首页，是最后一页
    {
        m_btnPreviousPage->setEnabled(true);
        m_btnFirstPage->setEnabled(true);
        m_btnNextPage->setEnabled(false);
        m_btnLastPage->setEnabled(false);
    }
    else if(m_nCurPageNum != m_nTotalPageNum && m_nCurPageNum == 1) //非最后一页，且是首页
    {
        m_btnPreviousPage->setEnabled(false);
        m_btnFirstPage->setEnabled(false);
        m_btnNextPage->setEnabled(true);
        m_btnLastPage->setEnabled(true);
    }
    else
    {
        m_btnPreviousPage->setEnabled(true);
        m_btnNextPage->setEnabled(true);
        m_btnLastPage->setEnabled(true);
        m_btnFirstPage->setEnabled(true);
    }


    {
        //发送消息
        qDebug() << m_nCurPageNum;
        emit PageNumChange(m_nCurPageNum);
    }
}

void CPageCtrl::OnLineEditTextEnter()
{
    if(!m_editCurPageNum)
    {
        return;
    }
    m_editCurPageNum->SetText2(m_editCurPageNum->text());
}

void CPageCtrl::SetTotalCount(const int & nTotalPageCount, const int & nTotalItemCount, const int & nCurPageCount)
{
    m_nTotalPageNum = nTotalPageCount;
    m_nTotalItemNum = nTotalItemCount;
    m_nCurPageNum = nCurPageCount;

    if(m_editCurPageNum)
    {
        m_editCurPageNum->setText(QString::number(nCurPageCount, 10));
    }

    if(m_pLabTotalPageCount)
    {
        QString strtext;
        strtext = "共";
        strtext += QString::number(m_nTotalPageNum, 10);
        strtext += "页 ";

        strtext += "共";
        strtext += QString::number(m_nTotalItemNum, 10);
        strtext += "项";
        m_pLabTotalPageCount->setText(strtext);
    }

    if(!m_btnPreviousPage || !m_btnNextPage || !m_btnLastPage || !m_btnFirstPage)
    {
        return;
    }

    //cur == total , cur == 1
    if(m_nCurPageNum == m_nTotalPageNum && m_nCurPageNum == 1)
    {
        m_btnPreviousPage->setEnabled(false);
        m_btnNextPage->setEnabled(false);
        m_btnLastPage->setEnabled(false);
        m_btnFirstPage->setEnabled(false);
    }
    else if(m_nCurPageNum == m_nTotalPageNum && m_nCurPageNum != 1)//非首页，是最后一页
    {
        m_btnPreviousPage->setEnabled(true);
        m_btnFirstPage->setEnabled(true);
        m_btnNextPage->setEnabled(false);
        m_btnLastPage->setEnabled(false);
    }
    else if(m_nCurPageNum != m_nTotalPageNum && m_nCurPageNum == 1) //非最后一页，且是首页
    {
        m_btnPreviousPage->setEnabled(false);
        m_btnFirstPage->setEnabled(false);
        m_btnNextPage->setEnabled(true);
        m_btnLastPage->setEnabled(true);
    }
    else
    {
        m_btnPreviousPage->setEnabled(true);
        m_btnNextPage->setEnabled(true);
        m_btnLastPage->setEnabled(true);
        m_btnFirstPage->setEnabled(true);
    }

}

int CPageCtrl::GetCurPageNum()
{
    return m_nCurPageNum;
}

void CPageCtrl::SetCurPageNum(const int & nCurpPageNum)
{
    if(!m_editCurPageNum)
    {
        return;
    }
    m_editCurPageNum->SetText2(QString::number(nCurpPageNum,10));
}
